package com.swiftops.basic.sys.service.impl;

import com.swiftops.common.constant.CommonEnum;
import com.swiftops.common.service.AbstractService;
import com.swiftops.common.core.PageResult;
import com.swiftops.common.util.IdGen;
import com.swiftops.common.util.ListUtil;
import com.swiftops.common.constant.CommonConstant;
import com.swiftops.basic.sys.dao.SysConfigMapper;
import com.swiftops.basic.sys.model.SysConfig;
import com.swiftops.basic.sys.service.SysConfigService;
import com.swiftops.basic.sys.vo.entity.SysConfigEntityVO;
import com.swiftops.basic.sys.vo.request.SysConfigRequestVO;
import com.swiftops.basic.sys.vo.response.SysConfigResponseVO;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;

/**
 * 系统配置表
 *
 * @author yangyang
 * @date 2023/09/17
 **/
@Slf4j
@Service
@Transactional
public class SysConfigServiceImpl extends AbstractService<SysConfig> implements SysConfigService {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 获取系统配置表分页列表
     *
     * @param sysConfigRequestVO 系统配置表查询条件
     * @return pageResult 系统配置表分页结果
     */
    @Override
    public PageResult<SysConfigResponseVO> querySysConfigPageList(SysConfigRequestVO sysConfigRequestVO) {
        //
        if (sysConfigRequestVO.getPageNumber() == null) sysConfigRequestVO.setPageNumber(CommonConstant.DEFAULT_PAGENUMBER);
        if (sysConfigRequestVO.getPageSize() == null) sysConfigRequestVO.setPageSize(CommonConstant.DEFAULT_PAGESIZE);
        //
        List<SysConfigResponseVO> sysConfigResponseVOList = Collections.emptyList();
        Condition condition = new Condition(SysConfig.class);
        Example.Criteria criteria = condition.createCriteria();
        //
        //if (StringUtils.isNotBlank(sysConfigRequestVO.getDesc())) {
        //    criteria.andLike("userName", "%" + sysConfigRequestVO.getDesc() + "%");
        //}
        //删除状态
        criteria.andEqualTo("deleteFlag", CommonEnum.StateFlagEnum.STATE_FLAG_NORMAL.getCode());
        //
        int total = this.selectCountByCondition(condition);
        if (total > 0) {
            PageHelper.startPage(sysConfigRequestVO.getPageNumber(), sysConfigRequestVO.getPageSize());
            PageHelper.startPage(11,22,false,false,false);
            List<SysConfig> sysConfigList = this.selectByCondition(condition);
            sysConfigResponseVOList = ListUtil.convertList(sysConfigList, SysConfigResponseVO.class);
        }
        //
        return PageResult.of(total, sysConfigResponseVOList);
    }

    /**
     * 获取系统配置表详细信息
     *
     * @param id 系统配置表ID
     * @return 系统配置表
     */
    @Override
    public SysConfigEntityVO getSysConfigEntityVO(String id) {
        SysConfig sysConfig = this.selectByPrimaryKey(id);
        if (null == sysConfig) return null;

        SysConfigEntityVO sysConfigEntityVO = new SysConfigEntityVO();
        BeanUtils.copyProperties(sysConfig, sysConfigEntityVO);
        return sysConfigEntityVO;
    }

    /**
     * 添加系统配置表
     *
     * @param sysConfigEntityVO 系统配置表
     */
    @Override
    public void addSysConfig(SysConfigEntityVO sysConfigEntityVO) {
        SysConfig sysConfig = new SysConfig();
        BeanUtils.copyProperties(sysConfigEntityVO, sysConfig);
        sysConfig.setId(IdGen.next());
        this.insertSelective(sysConfig);
    }

    /**
     * 修改系统配置表
     *
     * @param sysConfigEntityVO 系统配置表
     */
    @Override
    public void updateSysConfig(SysConfigEntityVO sysConfigEntityVO) {
        SysConfig sysConfig = new SysConfig();
        BeanUtils.copyProperties(sysConfigEntityVO, sysConfig);
        this.updateByPrimaryKeySelective(sysConfig);
    }

    /**
     * @param id        主键
     * @param isUseable 启用状态
     */
    @Override
    public void useableSysConfig(String id, Boolean isUseable) {
        SysConfig sysConfig = new SysConfig();
        sysConfig.setId(id);
        Byte useable = isUseable ? CommonEnum.EnableStateEnum.ENABLE_STATE_YES.getCode() : CommonEnum.EnableStateEnum.ENABLE_STATE_NO.getCode();
        sysConfig.setUseable(useable);
        this.updateByPrimaryKeySelective(sysConfig);
    }

    /**
     * 删除系统配置表
     *
     * @param id 系统配置表ID
     */
    @Override
    public void deleteSysConfig(String id) {
        SysConfig sysConfig = new SysConfig();
        sysConfig.setId(id);
        //sysConfig.setStateFlag(CommonEnum.StateFlagEnum.STATE_FLAG_DELETE.getCode());
        //逻辑删除
        this.updateByPrimaryKeySelective(sysConfig);
    }
}
